﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;

namespace Manul.Genetics.Genom
{
    public class Locus : GenomItem
    {
        public List<Gen> Gens = new List<Gen>();

        public int Capacity
        { get; set; }
        public LocusPlaceType PlaceType
        { get; set; }

        public Locus()
        {
            PlaceType = LocusPlaceType.XY;
            Capacity = 2;
        }

        public override NamedItem FindByGuid(string guid)
        {
            NamedItem namedItem = base.FindByGuid(guid);
            if (namedItem != null)
                return namedItem;
            foreach (Gen gen in Gens)
                if ((namedItem = gen.FindByGuid(guid)) != null)
                    return namedItem;
            return null;
        }

        public override Gen FindGen(string code)
        {
            Gen resultGen = Gen.Null;

            foreach (Gen gen in Gens)
            {
                if (gen.Code == code)
                {
                    resultGen = gen;
                    break;
                }
            }
            return resultGen;
        }

        public override void Swap(int index1, int index2)
        {
            if (IsInList(index1) && IsInList(index2))
            {
                Gen genItem = Gens[index1];
                Gens.RemoveAt(index1);
                Gens.Insert(index2, genItem);
            }
        }
        public override bool IsInList(int index)
        {
            return index >= 0 && index < Gens.Count;
        }
    }

    public enum LocusPlaceType
    {
        X = 1,
        Y = 2,
        XY = 3
    }
}
